#ifndef DROIDMEDIAPLAYER_JNI_PLAYER_H
#define DROIDMEDIAPLAYER_JNI_PLAYER_H


#include <jni.h>
#include <stddef.h>
#include <unistd.h>
#include <list>
#include "common.h"
#include "gl_render.h"
#include "mediaplayer/mediaplayer.h"

JavaVM *gVm;
jobject globalThizRef;

int jniThrowException(JNIEnv* env, const char* className, const char* msg);

JNIEnv* getJNIEnv();

int jniRegisterNativeMethods(JNIEnv* env,
                             const char* className,
                             const JNINativeMethod* gMethods,
                             int numMethods);



void initMediaPlayer(JNIEnv* env, jobject thiz, jobject weak_thiz);
MediaPlayer* getMediaPlayer(JNIEnv* env, jobject thiz);
MediaPlayer* setMediaPlayer(JNIEnv* env, jobject thiz, MediaPlayer* player);

void onVideoDecoded(AVFrame* frame, double pts, int width, int height);



// ----------------------------------------------------------------------------
// ref-counted object for callbacks
class JNIFFmpegMediaPlayerListener: public MediaPlayerListener
{
public:
    JNIFFmpegMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
    ~JNIFFmpegMediaPlayerListener();
    void notify(int msg, int ext1, int ext2, int ext3);
private:
    JNIFFmpegMediaPlayerListener();
    jclass      mClass;     // Reference to MediaPlayer class
    jobject     mObject;    // Weak ref to MediaPlayer Java object to call on
};

// ----------------------------------------------------------------------------


void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message);

pthread_mutex_t mLock;

char * pFrame;
int mVideoWidth;
int mVideoHeight;

bool mFrameUpdated;

typedef struct {
    AVFrame* frame;
    double pts;
    int seq;
}AVFrameRef;

uint8_t*                    video_dst_data[4];
int                         video_dst_linesize[4];
int                         video_dst_bufsize;

int mSeq;

bool mImageAllocated;

std::list<AVFrameRef*> mFrameList;

pthread_t mRefreshThread;

void startRefresh();
void* refreshTimer(void* ptr);

static FILE *video_dst_file;

#endif //DROIDMEDIAPLAYER_JNI_PLAYER_H
